package edu.bu.cs511.p5;

import java.awt.Color;

/** 
 * This opcode is to be used along with the SchedulerDemo class.  It
 * will report to the SchedulerDemo class when it runs so that
 * cumulative proportions of execution share can be visualized.  Thus
 * this opcode should be used only in conjunction with the
 * SchedulerDemo.
 */
public class DemoOpcode implements IOpcode {
    /** Identification for this opcode. */
    private int id;
    private int timeout;
    private Color color;

    /**
     * Constructor to create the DemoTask.<p/>
     *
     * @REQUIRES id is an identification of this opcode to be reported
     * in SchedulerDemo, c is the color to print this task as, and
     * timeout is the amount of time this opcode should execute every
     * time it is invoked.
     */
    public DemoOpcode(int id, int timeout, Color c) {
        this.id = id;
	this.color = c;
        this.timeout = timeout;
    }

    /** 
     * @MODIFIES: Execute for a time defined by the timeout pass in to
     * the constructor and notify the SchedulerDemo class that we have
     * executed.
     */
    public void execute() {
        try {
            Thread.sleep(timeout);
        } catch (Exception e) {
            System.out.println("e = " + e);
        }
        SchedulerDemo.notifyOpcodeExecution(id, color);
    }

    /**
     * An accessor method return if the opcode is currently enabled or
     * not.</p>
     *
     * @EFFECTS return true if the opcode is enabled, false otherwise.
     */
    public boolean isEnabled() {
        return true;
    }

}
